package com.ruoyi.fyhs.service.impl.lld;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fyhs.domain.lld.KtLldMx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.fyhs.domain.lld.KtLldFj;
import com.ruoyi.fyhs.mapper.lld.KtLldMapper;
import com.ruoyi.fyhs.domain.lld.KtLld;
import com.ruoyi.fyhs.service.lld.IKtLldService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 费用核算领料单Service业务层处理
 *
 * @author huacai
 * @date 2024-04-17
 */
@Service
public class KtLldServiceImpl implements IKtLldService
{
    @Autowired
    private KtLldMapper ktLldMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询费用核算领料单
     *
     * @param lldId 费用核算领料单主键
     * @return 费用核算领料单
     */
    @Override
    public KtLld selectKtLldByLldId(Integer lldId)
    {
        return ktLldMapper.selectKtLldByLldId(lldId);
    }

    /**
     * 查询费用核算领料单列表
     *
     * @param ktLld 费用核算领料单
     * @return 费用核算领料单
     */
    @Override
    public List<KtLld> selectKtLldList(KtLld ktLld)
    {
        return ktLldMapper.selectKtLldList(ktLld);
    }

    /**
     * 新增费用核算领料单
     *
     * @param ktLld 费用核算领料单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertKtLld(KtLld ktLld)
    {
        ktLld.setCreateTime(DateUtils.getNowDate());
        int rows = ktLldMapper.insertKtLld(ktLld);
        insertKtLldFj(ktLld);
        insertKtLldMx(ktLld);
        return rows;
    }

    /**
     * 批量新增费用核算领料单
     *
     * @param ktLlds 费用核算领料单List
     * @return 结果
     */
    @Override
    public int batchInsertKtLld(List<KtLld> ktLlds)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(ktLlds)) {
            try {
                for (int i = 0; i < ktLlds.size(); i++) {
                    int row = ktLldMapper.insertKtLld(ktLlds.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == ktLlds.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改费用核算领料单
     *
     * @param ktLld 费用核算领料单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateKtLld(KtLld ktLld)
    {
        ktLld.setUpdateTime(DateUtils.getNowDate());
        ktLldMapper.deleteKtLldFjByLldId(ktLld.getLldId());
        ktLldMapper.deleteKtLldMxByLldId(ktLld.getLldId());
        insertKtLldFj(ktLld);
        insertKtLldMx(ktLld);
        return ktLldMapper.updateKtLld(ktLld);
    }

    /**
     * 批量删除费用核算领料单
     *
     * @param lldIds 需要删除的费用核算领料单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteKtLldByLldIds(Integer[] lldIds)
    {
        ktLldMapper.deleteKtLldFjByLldIds(lldIds);
        ktLldMapper.deleteKtLldMxByLldIds(lldIds);
        return ktLldMapper.deleteKtLldByLldIds(lldIds);
    }

    /**
     * 删除费用核算领料单信息
     *
     * @param lldId 费用核算领料单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteKtLldByLldId(Integer lldId)
    {
        ktLldMapper.deleteKtLldFjByLldId(lldId);
        ktLldMapper.deleteKtLldMxByLldId(lldId);
        return ktLldMapper.deleteKtLldByLldId(lldId);
    }

    /**
     * 新增领料单附件信息
     *
     * @param ktLld 费用核算领料单对象
     */
    public void insertKtLldFj(KtLld ktLld)
    {
        List<KtLldFj> ktLldFjList = ktLld.getKtLldFjList();
        Integer lldId = ktLld.getLldId();
        if (StringUtils.isNotNull(ktLldFjList))
        {
            List<KtLldFj> list = new ArrayList<KtLldFj>();
            for (KtLldFj ktLldFj : ktLldFjList)
            {
                ktLldFj.setLldId(lldId);
                list.add(ktLldFj);
            }
            if (list.size() > 0)
            {
                ktLldMapper.batchKtLldFj(list);
            }
        }
    }

    /**
     * 新增领料明细信息
     *
     * @param ktLld 费用核算领料单对象
     */
    public void insertKtLldMx(KtLld ktLld)
    {
        List<KtLldMx> ktLldMxList = ktLld.getKtLldMxList();
        Integer lldId = ktLld.getLldId();
        if (StringUtils.isNotNull(ktLldMxList))
        {
            List<KtLldMx> list = new ArrayList<KtLldMx>();
            for (KtLldMx ktLldMx : ktLldMxList)
            {
                ktLldMx.setLldId(lldId);
                list.add(ktLldMx);
            }
            if (list.size() > 0)
            {
                ktLldMapper.batchKtLldMx(list);
            }
        }
    }

    /**
     * 新增附件
     * @param ktLldFj
     * @return
     */
    @Override
    public int insertKtLldFjByxmsbId(KtLldFj ktLldFj) {
        return ktLldMapper.insertKtLldFjByxmsbId(ktLldFj);
    }

    /**
     * 根据主表ID查询附件列表
     * @param ktLldFj
     * @return
     */
    @Override
    public List<KtLldFj> selectKtLldFjList(KtLldFj ktLldFj) {
        return ktLldMapper.selectKtLldFjList(ktLldFj);
    }

    /**
     * 获取领料单明细详细信息
     * @param lldId
     * @return
     */
    @Override
    public KtLld selectKtLldMxByLldId(Integer lldId) {
        return ktLldMapper.selectKtLldMxByLldId(lldId);
    }

}
